﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace InpValidator
{
    public class CompositValidator: ValidatorBase
    {
        public CompositValidator() : base()
        {
            childs.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(childs_CollectionChanged);

            foreach (ValidatorBase Validator in childs)
            {
                Initialize();
            }
        }

        void childs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            this.Initialize();
        }
        
        private ObservableCollection<ValidatorBase> childs = new ObservableCollection<ValidatorBase>();

        public ObservableCollection<ValidatorBase> Childs
        {
            get 
            {
                return childs; 
            }
            set
            {
                childs = value; 
            }
        }

        protected override void Initialize()
        {
            foreach (ValidatorBase Validator in childs)
            {
                Validator.LayoutRoot = this.LayoutRoot;
                Validator.ElementToValidate = this.ElementToValidate;
            }
        }
        
        public override bool ValidateControl()
        {
            this.ErrorMessage = "";
            bool flag = true;
            foreach (ValidatorBase Validator in childs)
            {
                Validator.ElementToValidate = this.ElementToValidate;
                this.ErrorMessage += Validator.ErrorMessage + " ";
                flag = flag && Validator.ValidateControl();
            }
            return flag;
        }
    }
}
